package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.PolicyCourierTypeAddDTO;
import org.jsola.hr.dto.PolicyCourierTypeUpdateDTO;
import org.jsola.hr.dto.UpdatePolicySortDTO;
import org.jsola.hr.query.PolicyCourierTypeQuery;
import org.jsola.hr.service.IPolicyCourierTypeService;
import org.jsola.hr.vo.PolicyCourierTypeListVO;
import org.jsola.hr.vo.PolicyCourierTypeVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 政策速递类型
 *
 * @author zhr
 */
@RestController("hrPolicyCourierTypeController")
@RequestMapping(HrConstants.API_PATH + "/v1/policyCourierTypes")
public class PolicyCourierTypeController implements IPolicyCourierTypeControllerAPI {

    @Autowired
    private IPolicyCourierTypeService policyCourierTypeService;

    @Override
    @PostMapping
    public Result<PolicyCourierTypeVO> save(@Valid @RequestBody PolicyCourierTypeAddDTO policyCourierTypeAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增政策速递类型权限校验

        return Result.success(
                policyCourierTypeService.save(policyCourierTypeAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody PolicyCourierTypeUpdateDTO policyCourierTypeUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改政策速递类型权限校验

        int count = policyCourierTypeService.update(policyCourierTypeUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{policyCourierTypeId}")
    public Result<Integer> delete(@PathVariable String policyCourierTypeId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除政策速递类型权限校验

        int count = policyCourierTypeService.deleteByIds(tokenUser, policyCourierTypeId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] policyCourierTypeIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除政策速递类型权限校验

        int count = policyCourierTypeService.deleteByIds(tokenUser, policyCourierTypeIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{policyCourierTypeId}")
    public Result<PolicyCourierTypeVO> findById(@PathVariable String policyCourierTypeId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看政策速递类型权限校验

        return Result.success(
            policyCourierTypeService.selectById(policyCourierTypeId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<PolicyCourierTypeListVO>> listAll(@Valid PolicyCourierTypeQuery policyCourierTypeQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看政策速递类型权限校验

        return Result.success(
            policyCourierTypeService.select(policyCourierTypeQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<PolicyCourierTypeListVO>> page(@Valid PolicyCourierTypeQuery policyCourierTypeQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            policyCourierTypeService.selectPage(policyCourierTypeQuery, tokenUser.getSiteId())
        );
    }

    @PutMapping("/updateSort")
    public Result<String> updateSort(@RequestBody List<UpdatePolicySortDTO> updateSortList) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        policyCourierTypeService.updateSort(updateSortList);
        return Result.success();
    }
}

